﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConjureBivouac2012.GameStates;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework;

namespace ConjureBivouac2012.Object
{
    public class MonsterC : AbstractEnemy
    {
        bool newBulletNeeded = false;
        private float fireDelay;
        private double lastShootFired;

        public MonsterC(float fireDelay, double lastShootFired) : base(80, 5)
        {
            this.fireDelay = fireDelay;
            this.lastShootFired = lastShootFired;
        }

        // Return a new bullet that has been shoteed
        public override Entity[] addingEntity()
        {
            BigBullet[] bullets = new BigBullet[1];
            if (newBulletNeeded)
            {
                newBulletNeeded = false;

                for (int i = 0; i < 1; i++)
                {
                    BigBullet b = new BigBullet();
                    bullets[i] = b;
                }

                float directionX = (float)random.Next(-100, 100) / 100;
                float directionY = (float)random.Next(-100, 100) / 100;
                bullets[0].Fire(this, new Vector2(directionX, directionY));
                return bullets;
            }
            else return null;
        }

        public override void LoadContent(ContentManager contentManager)
        {
            contentName = "Ennemy/EnnemyC";
            this.sprite.LoadContent(contentManager, contentName);
            base.LoadContent(contentManager);
        }

        public override void Update(GameTime time)
        {
            base.Update(time);
            Fire(time);
        }

        public void Fire(GameTime time)
        {
            if (fireDelay > 0 && (lastShootFired == -1 || time.TotalGameTime.TotalSeconds - lastShootFired >= fireDelay))
            {
                lastShootFired = time.TotalGameTime.TotalSeconds;
                newBulletNeeded = true;
            }
        }

        private Vector2 GetFireDirection()
        {
            Vector2 direction = new Vector2(0, 0);

            IGlobalGameState state = Program.GetGameInstance.GetCurrentState();

            AbstractPlayer p = null;

            if (state is PlayGame)
            {
                p = ((PlayGame)state).GetPlayer();
            }

            if (p != null)
            {
                direction = p.GetVehicule().sprite.Position - this.sprite.Position;
                direction.Normalize();
            }

            return direction;
        }
    }
}
